//Player Entity
var PlayerEntity = me.ObjectEntity.extend(
{    

        init:function (x, y, settings)
        {
            // call the constructor
            this.parent(x, y , settings);
			// character SPEED
			this.walkSpeed = 1;
            // set the walking speed
            this.setVelocity(1, 1);

            var tileIs = getTileEntityIs(this);
            this.pos.x = tileIs.x*16;
            this.pos.y = tileIs.y*16+8;
			// set the walking speed
            this.setFriction(0.0, 0.0);
            
            // adjust the bounding box
            this.updateColRect(1,1,5,5);
            
            // disable gravity
            this.gravity = 0;
            
            //this.firstUpdates = 0;
            this.direction = 'down';
         
			//check when the character is moving
			this.moving = false;

            this.canMove = true;

			//how many pixels still to move
			this.pixelsToMove = 16;
            this.animationspeed = me.sys.fps / 15;

            // set the display to follow our position on both axis
            me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
            
            this.addAnimation("stand-down", [0]);
            this.addAnimation("stand-left", [3]);
            this.addAnimation("stand-up", [6]);
            this.addAnimation("stand-right", [11]);
            this.addAnimation("down", [0,1,2]);
            this.addAnimation("left", [3,4,5]);
            this.addAnimation("up", [6,7,8]);
            this.addAnimation("right", [9,10,11]);

        },

        // Update player position.
        update : function ()
        {
            hadSpeed = this.vel.y != 0 || this.vel.x != 0

            if (me.input.isKeyPressed('right') && this.moving==false)
            {
                this.setCurrentAnimation('right')
                this.direction = 'right'
                if(canMove(1,0,this)) {

                    this.moving = true;
                }
            } else if(me.input.isKeyPressed('left') && this.moving==false) {
                this.setCurrentAnimation('left')
                this.direction = 'left'
                if(canMove(-1,0,this)) {


                    this.moving = true;
                }
            } else if(me.input.isKeyPressed('up') && this.moving==false) {
                this.setCurrentAnimation('up')
                this.direction = 'up'
                if(canMove(0,-1,this)) {
                    this.moving = true;
                }
            } else if(me.input.isKeyPressed('down') && this.moving==false) {
                this.setCurrentAnimation('down')
                this.direction = 'down'
                if(canMove(0,1,this)) {
                    this.moving = true;
                }
            }

            if(me.input.isKeyPressed('debug')) {
                var pos = getTileEntityIs(this);
                alert("X "+pos.x+"Y "+pos.y);
            }

            if(this.moving == true) {
                    switch(this.direction) {
                        case 'right' :
                            this.vel.x = moveEntity(this,this.vel.x, 1);
                            break;
                        case  'left' :
                            this.vel.x = moveEntity(this,this.vel.x, -1);
                            break;
                        case  'up' :
                            this.vel.y = moveEntity(this,this.vel.y, -1);
                            break;
                        case  'down' :
                            this.vel.y = moveEntity(this,this.vel.y, 1);
                            break;
                    }
            }
		
            // check & update player movement
            updated = this.updateMovement();
            
			if (this.moving == false && this.vel.y == 0 && this.vel.x == 0)
            {
                this.setCurrentAnimation('stand-' + this.direction);
            }
		   
			// check for collision
			res = me.game.collide(this);

            // update animation
            if (updated)
            {
                // update object animation
                this.parent(this);
            }
            return updated;
        }

});

